package handler

import (
	"encoding/json"
	"litetalk/api/form"
	"litetalk/proto"

	"github.com/gin-gonic/gin"
)

type ApiHandler struct {
}

func (*ApiHandler) PushUserMessage(c *gin.Context) {

	form := &proto.IMMessage{}

	if err := c.ShouldBindJSON(form); err != nil {
		FailWithMsg(c, err.Error())
		return
	}
	body, _ := json.Marshal(form)
	msg := proto.NewMsg(proto.OpSingleSend, body, 0)

	reply := &proto.SuccessReply{}
	if err := logicRpc.Push(msg, reply); err != nil {
		FailWithMsg(c, err.Error())
		return
	}
	SuccessWithMsg(c, "success", nil)
}

func (*ApiHandler) PushGroupMessage(c *gin.Context) {
	form := &proto.IMMessage{}
	if err := c.ShouldBindJSON(form); err != nil {
		FailWithMsg(c, err.Error())
		return
	}
	body, _ := json.Marshal(form)
	msg := proto.NewMsg(proto.OpGroupSend, body, 0)

	reply := &proto.SuccessReply{}
	if err := logicRpc.Push(msg, reply); err != nil {
		FailWithMsg(c, err.Error())
		return
	}

	SuccessWithMsg(c, "success", nil)
}

func (*ApiHandler) ChangeUserAvatar(c *gin.Context) {
	form := &proto.IMChangeAvatarReq{}
	if err := c.ShouldBindJSON(form); err != nil {
		FailWithMsg(c, err.Error())
		return
	}

	reply := &proto.IMChangeAvatarRes{}
	if err := logicRpc.ChangeAvatar(form, reply); err != nil {
		FailWithMsg(c, err.Error())
		return
	}

	replyJson, _ := json.Marshal(reply)
	SuccessWithMsg(c, "success", replyJson)
}

func (*ApiHandler) ChangeUserSigninfo(c *gin.Context) {
	form := &proto.IMChangeSignInfoReq{}
	if err := c.ShouldBindJSON(form); err != nil {
		FailWithMsg(c, err.Error())
		return
	}

	reply := &proto.IMChangeSignInfoRes{}
	if err := logicRpc.ChangeSignInfo(form, reply); err != nil {
		FailWithMsg(c, err.Error())
		return
	}

	replyJson, _ := json.Marshal(reply)
	SuccessWithMsg(c, "success", replyJson)
}

func (*ApiHandler) ChangeGroupAvatar(c *gin.Context) {
	form := &proto.IMGroupChangeAvatarReq{}
	if err := c.ShouldBindJSON(form); err != nil {
		FailWithMsg(c, err.Error())
		return
	}

	reply := &proto.IMGroupChangeAvatarRes{}
	if err := logicRpc.ChangeGroupAvatar(form, reply); err != nil {
		FailWithMsg(c, err.Error())
		return
	}

	replyJson, _ := json.Marshal(reply)
	SuccessWithMsg(c, "success", replyJson)
}

func (*ApiHandler) CreateDepart(c *gin.Context) {
	info := &proto.IMDepartInfo{}
	if err := c.ShouldBindJSON(info); err != nil {
		FailWithMsg(c, err.Error())
		return
	}

	result := &proto.IMDepartInfoAck{}
	if err := logicRpc.CreateDepart(info, result); err != nil {
		FailWithMsg(c, err.Error())
		return
	}

	SuccessWithMsg(c, "success", result)
}

func (*ApiHandler) UpdateDepart(c *gin.Context) {
	info := &proto.IMDepartInfo{}
	if err := c.ShouldBindJSON(info); err != nil {
		FailWithMsg(c, err.Error())
		return
	}

	result := &proto.IMDepartInfoAck{}
	if err := logicRpc.ChangeDepartInfoByNumber(info, result); err != nil {
		FailWithMsg(c, err.Error())
		return
	}

	SuccessWithMsg(c, "success", result)
}

func (*ApiHandler) DeleteDepartByOrigainze(c *gin.Context) {
	info := &proto.IMDepartOrganize{}
	if err := c.ShouldBindJSON(info); err != nil {
		FailWithMsg(c, err.Error())
		return
	}

	reply := &proto.SuccessReply{}
	if err := logicRpc.DeleteDepart(info, reply); err != nil {
		FailWithMsg(c, err.Error())
		return
	}

	SuccessWithMsg(c, "success", reply)
}

func (*ApiHandler) SystemMessage(c *gin.Context) {
	info := &form.FormMessageInfo{}
	if err := c.ShouldBindJSON(info); err != nil {
		FailWithMsg(c, err.Error())
	}

	var userInfoReq = proto.IMUserInfoNameReq{
		UserName: info.UserName,
	}

	var userInfoRes = proto.IMUserInfoNameRes{}
	if err := logicRpc.GetUserInfoByName(&userInfoReq, &userInfoRes); err != nil {
		FailWithMsg(c, err.Error())
		return
	}

	args := &proto.IMMessage{
		FromUserId: 1,
		SessionId:  userInfoRes.Data.Id,
		MsgData:    info.MsgData,
	}

	body, _ := json.Marshal(args)
	msg := proto.NewMsg(proto.OpSingleSend, body, 0)

	reply := &proto.SuccessReply{}
	if err := logicRpc.Push(msg, reply); err != nil {
		FailWithMsg(c, err.Error())
		return
	}
	SuccessWithMsg(c, "success", nil)
}

type InterfaceGroup interface {
	Group(relativePath string, handlers ...gin.HandlerFunc) *gin.RouterGroup
}

func (a *ApiHandler) Group(r InterfaceGroup) *gin.RouterGroup {

	api := r.Group("/api", ApiMiddleware())
	api.POST("/push/user", a.PushUserMessage)
	api.POST("/push/group", a.PushGroupMessage)
	api.PUT("/user/avatar", a.ChangeUserAvatar)
	api.PUT("/user/signinfo", a.ChangeUserSigninfo)
	api.PUT("/group/avatar", a.ChangeGroupAvatar)
	api.POST("/depart", a.CreateDepart)
	api.PUT("/depart", a.UpdateDepart)
	api.POST("/depart/delete", a.DeleteDepartByOrigainze)
	api.POST("/user", Register)
	api.PUT("/user", UpdatePassword)
	api.POST("/user/delete", DeleteUser)
	api.POST("/push/system", a.SystemMessage)

	return api
}
